import fetch from "node-fetch";
import PostCard from "@/components/postCard/postCard";
import styles from "./blog.module.css";
// import { getPosts } from "@/lib/data";

const getData = async () => {
  // default cache
  // const res = await fetch("https://jsonplaceholder.typicode.com/posts", {});
  const res = await fetch("http://localhost:3000/api/blog", {});
  if (!res.ok) throw new Error("Something went wrong");
  return res.json();
};

export const metadata = {
  title: "blog page",
  description: "blog desc",
};

const Blog = async () => {
  const posts = await getData();
  // const posts = await getPosts();
  if (!posts.length) {
    return <div>empty</div>;
  }
  return (
    <div className={styles.container}>
      {posts.map((post) => {
        return (
          <div className={styles.post} key={post.id}>
            <PostCard post={post} />
          </div>
        );
      })}
    </div>
  );
};

export default Blog;
